{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Train and deploy with private registry on Kubeflow from Notebooks\n",
    "\n",
    "### In order to use a private registry in fairing append builder on the Jupyter notebook, two settings are required.\n",
    "- Copy the .docker/config.json containing the private registry connection information to /home/jovyan/.docker/config.json on Jupyter-notebook container for docker-image-push by the append builder.\n",
    "    ```\n",
    "    # under docker client environ.\n",
    "    $ docker login <private_registry>\n",
    "    $ cat ~/.docker/config.json\n",
    "      \n",
    "    # in notebook console\n",
    "    $ mkdir /home/jovyan/.docker\n",
    "    $ vi /home/jovyan/.docker/config.json\n",
    "    ```\n",
    "-  Patch imagePullSecrets on the service account so that the deployer can pull the private registry image.\n",
    "    ```\n",
    "    # in current context(or in notebook console)\n",
    "    $ kubectl create secret generic regcred --from-file=.dockerconfigjson=/home/${USER}/.docker/config.json \\\n",
    "    --type=kubernetes.io/dockerconfigjson -n anonymous  \n",
    "    $ kubectl patch serviceaccount default -p '{\"imagePullSecrets\": [{\"name\": \"regcred\"}]}' -n anonymous\n",
    "    ```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from kubeflow import fairing\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DOCKER_REGISTRY = '<private_registry>'\n",
    "fairing.config.set_builder('append',base_image='gcr.io/kubeflow-images-public/tensorflow-1.14.0-notebook-cpu:v0.7.0', registry=DOCKER_REGISTRY, push=True)\n",
    "fairing.config.set_deployer('job')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train():\n",
    "    print(\"hello world!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Training on notebook \n",
    "train()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Training on current kubernetes context.\n",
    "remote_train = fairing.config.fn(train)\n",
    "remote_train()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}